<template>
  <header class="tencent-header" :class="{ 'scrolled': !isTop }">
    <div class="header-container">
      <div class="logo">
        <router-link to="/">
          <img src="../assets/images/logo.png" alt="Tencent" />
        </router-link>
      </div>
      <el-menu
          :default-active="activeIndex"
          class="el-menu-demo"
          mode="horizontal"
          @select="handleSelect"
          :background-color="isTop ? 'transparent' : '#fff'"
          :text-color="isTop ? '#fff' : '#333'"
          :active-text-color="isTop ? '#ffd04b' : '#409EFF'"
      >
        <el-menu-item index="1">首页</el-menu-item>
        <el-sub-menu index="2">
          <template #title>代言</template>
          <el-menu-item index="2-1">沪上阿姨</el-menu-item>
          <el-menu-item index="2-2">Daneva</el-menu-item>
          <el-menu-item index="2-3">Mac</el-menu-item>
        </el-sub-menu>
        <el-menu-item index="3">作品集</el-menu-item>
        <el-menu-item index="4">制作服务</el-menu-item>
        <el-menu-item index="5">艺术家</el-menu-item>
        <el-menu-item index="6">新闻</el-menu-item>
        <el-menu-item index="7">关于我们</el-menu-item>
      </el-menu>
    </div>
  </header>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { Search } from '@element-plus/icons-vue'

const activeIndex = ref('1')
const searchText = ref('')
const isTop = ref(true)
const router = useRouter()

const handleScroll = () => {
  isTop.value = window.scrollY < 10
}

onMounted(() => {
  window.addEventListener('scroll', handleScroll)
})

onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll)
})

const handleSelect = (key) => {
  switch (key) {
    case '1':
      router.push('/')
      break
    case '2-1':
      router.push('/products/hsay')
      break
    case '2-2':
      router.push('/products/daneva')
      break
    case '2-3':
      router.push('/products/mac')
      break
    case '3':
      router.push('/opus')
      break
    case '4':
      router.push('/production')
      break
    case '5':
      router.push('/artists')
      break
    case '6':
      router.push('/news')
      break
    case '7':
      router.push('/about')
      break
  }
}
</script>

<style scoped>
.tencent-header {
  position: fixed;
  width: 100%;
  top: 0;
  z-index: 1000;
  transition: all 0.3s ease;
}

.tencent-header:not(.scrolled) {
  background-color: transparent;
  box-shadow: none;
}

.tencent-header.scrolled {
  background-color: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.header-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.logo img {
  height: 40px;
  transition: all 0.3s ease;
}

.tencent-header:not(.scrolled) .logo img {
  filter: brightness(0) invert(1);
}

.tencent-header.scrolled .logo img {
  filter: none;
}

.el-menu-demo {
  flex: 1;
  border-bottom: none;
}

:deep(.el-menu) {
  background-color: transparent !important;
}

:deep(.el-menu--horizontal.el-menu) {
  border-bottom: none !important;
}

:deep(.el-menu--horizontal > .el-menu-item),
:deep(.el-menu--horizontal > .el-sub-menu .el-sub-menu__title) {
  height: 60px;
  line-height: 60px;
  transition: color 0.3s ease;
  background-color: transparent !important;
}

/* 取消所有状态下的背景色 */
:deep(.el-menu--horizontal > .el-menu-item:hover),
:deep(.el-menu--horizontal > .el-sub-menu:hover .el-sub-menu__title),
:deep(.el-menu--horizontal > .el-menu-item:focus),
:deep(.el-menu--horizontal > .el-menu-item:active),
:deep(.el-menu--horizontal > .el-menu-item.is-active),
:deep(.el-menu--horizontal > .el-sub-menu.is-active .el-sub-menu__title) {
  background-color: transparent !important;
}

.tencent-header:not(.scrolled) :deep(.el-menu--horizontal > .el-menu-item),
.tencent-header:not(.scrolled) :deep(.el-menu--horizontal > .el-sub-menu .el-sub-menu__title) {
  color: white !important;
}

.tencent-header.scrolled :deep(.el-menu--horizontal > .el-menu-item),
.tencent-header.scrolled :deep(.el-menu--horizontal > .el-sub-menu .el-sub-menu__title) {
  color: #333 !important;
}

.tencent-header.scrolled :deep(.el-menu--horizontal > .el-menu-item.is-active),
.tencent-header.scrolled :deep(.el-menu--horizontal > .el-sub-menu.is-active .el-sub-menu__title) {
  color: #409EFF !important;
  border-bottom-color: #409EFF !important;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-container {
    flex-direction: column;
    padding: 10px;
  }

  .el-menu-demo {
    width: 100%;
  }
}
</style>
